package com.chezai.client.utils;

import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;

import com.chezai.client.global.DeviceFactory;
import com.chezai.client.global.KillSelfService;
import com.chezai.client.global.TerminalApplication;

/**
 * Describe
 * Created by LZF on 2016/8/16.
 */
public class DeviceUtils {
    private final static int SPACE_TIME = 500;
    private static long[] mHints = new long[5];//连续点击logo次数
    private static long lastClickTime = 0;

    public static String createQRImage(String contentQR) {
        final String filePath = new QRCodeUtil().getQrCodeUrl();
        if (TextUtils.isEmpty(contentQR)) {
            return "";
        }
        boolean success = new QRCodeUtil().createQRImage(contentQR.trim(), 500, 500, null, filePath);
        if (success) {
            return filePath;
        }
        return "";
    }

    public synchronized static boolean isDoubleClick() {
        long currentTime = System.currentTimeMillis();
        boolean isDoubleClick;
        if (currentTime - lastClickTime > SPACE_TIME) {
            lastClickTime = currentTime;
            isDoubleClick = false;
        } else {
            isDoubleClick = true;
        }
        lastClickTime = currentTime;
        return isDoubleClick;
    }

    /**
     * 快速连续点击五次
     */
    public static boolean fastFiveClick() {
        System.arraycopy(mHints, 1, mHints, 0, mHints.length - 1);//把从第二位至最后一位之间的数字复制到第一位至倒数第一位
        mHints[mHints.length - 1] = SystemClock.uptimeMillis();//从开机到现在的时间毫秒数
        if (SystemClock.uptimeMillis() - mHints[0] <= 1000) {//连续点击之间间隔小于一秒，有效
            mHints = new long[5];
            return true;
        } else {
            return false;
        }
    }

    //获取生成确认密码信息
    public static String getSystemPassInfo(Context context) {
        if (null == context) {
            return "";
        }
        String time = System.currentTimeMillis() + "";
        String timestamp = time.substring(0, 7);
        String systemPassInfo = "imei:" + DeviceFactory.getInstance().getHardware().getImei(TerminalApplication.mContext) + "\n" + "序列号：" + DeviceFactory.hardware.getSerialNumber(context)
                + "\n" + "时间：" + timestamp;
        return systemPassInfo;
    }

    /**
     * 设置系统的时间是否需要自动获取
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static void setAutoDateTime(ContentResolver cr) {
        Settings.Global.putInt(cr, Settings.Global.AUTO_TIME, 1);
    }

    /**
     * 设置系统的时区是否自动获取
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static void setAutoTimeZone(ContentResolver cr) {
        Settings.Global.putInt(cr, Settings.Global.AUTO_TIME_ZONE, 1);
    }

    /**
     * 判断系统的时间是否自动获取的
     *
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static boolean isDateTimeAuto(ContentResolver cr) {
        try {
            return Settings.Global.getInt(cr, Settings.Global.AUTO_TIME) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 判断系统的时区是否是自动获取的
     *
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static boolean isTimeZoneAuto(ContentResolver cr) {
        try {
            return Settings.Global.getInt(cr, Settings.Global.AUTO_TIME_ZONE) > 0;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 重启整个APP
     *
     * @param context
     * @param Delayed 延迟多少毫秒
     */
    public static void restartAPP(Context context, long Delayed) {

        /**开启一个新的服务，用来重启本APP*/
        Intent intent = new Intent(context, KillSelfService.class);
        intent.putExtra("packageName", context.getPackageName());
        intent.putExtra("Delayed", Delayed);
        context.startService(intent);

        /**杀死整个进程**/
        AppManager.getAppManager().AppExit(context, true);
    }

    /***重启整个APP*/
    public static void restartAPP(Context context) {
        restartAPP(context, 1000);
    }

}
